package ua.dogar.patterns;

import java.util.Iterator;

import ua.dogar.vectors.Vector;




public class JVectorAdapter implements Vector {
	java.util.Vector<Double> jVector;
	public JVectorAdapter(java.util.Vector<Double> jVector){
		this.jVector = jVector;
	}
	@Override
	public Iterator<Double> iterator() {
		return jVector.iterator();
	}

	@Override
	public double getElement(int index) {
		return jVector.get(index);
	}

	@Override
	public void setElement(double value, int index) {
		jVector.set(index, value);
	}

	@Override
	public int getLength() {
		return jVector.capacity();
	}

	@Override
	public void setElementsFromArray(double[] arr) {
		for(Double value:arr) jVector.add(value);
		
	}

	@Override
	public void setElementsFromVector(Vector listVec) {
		for(Double value:listVec) jVector.add(value);
	}

	@Override
	public Vector mulVector(int num) {
		for (int i = 0; i < getLength(); i++)
			setElement(getElement(i) * num, i);
		return this;
	}

	@Override
	public void addVector(Vector v) {
		if (v.getLength() == getLength()) {
			for (int i = 0; i < v.getLength(); i++)
				setElement(getElement(i) + v.getElement(i), i);
		} else
			System.out.println("Vectors not equals");
	}

	@Override
	public boolean compare(Vector listVec) {
		if (listVec.getLength() == getLength()) {
			for (int i = 0; i < listVec.getLength(); i++)
				if (getElement(i) != listVec.getElement(i))
					return false;
			return true;
		} else
			return false;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		JVectorAdapter clone = (JVectorAdapter) super.clone();
		clone.jVector = (java.util.Vector<Double>) this.jVector.clone();
		return clone;
	}
	@Override
	public int hashCode() {
		int result = 0;
		for (int i = 0; i < getLength(); i++) {
			long bits = Double.doubleToRawLongBits(getElement(i));
			result ^= ((int) (bits & 0x00000000FFFFFFFFL))
					^ ((int) ((bits & 0xFFFFFFFF00000000L)));
		}
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (obj instanceof Vector) {
			Vector temp = (Vector) obj;
			if (temp.getLength() == getLength()) {
				for (int i = 0; i < getLength(); i++)
					if (temp.getElement(i) != this.getElement(i))
						return false;
				return true;
			}
		}
		return false;
	}
	@Override
	public String toString() {
		StringBuffer stringOut = new StringBuffer();
		stringOut.append("JVector [vector=");
		stringOut.append(jVector.toString());
		stringOut.append("]");
		return stringOut.toString();
	}
	@Override
	public void registerObserver(Observer o) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void removeObserver(Observer o) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void notifyObservers(int index) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		
	}

}
